

import java.io.IOException;
import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pool.bean.Transaction_Bean;
import control.RequestParameter;
import dbmanager.DBManager;
import dbmanager.GetIdName;

/**
 * Servlet implementation class for Servlet: TransactionInsertServlet
 *
 */
 public class TransactionInsertServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public TransactionInsertServlet() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{		
		GetIdName get  = new GetIdName();
        
        String master = null,username = null,password = null;
        boolean sendresp = false;
        
        HttpSession session = request.getSession(true);
        username = (String)session.getAttribute("username");
        password = (String)session.getAttribute("password");
        
		try 
		{
			String transaction_type = (String)session.getAttribute("transaction_type");
        	String buyer = request.getParameter("buyer");
        	String seller = request.getParameter("seller"); 
        	String delivery_date = request.getParameter("delivery_date");
        	
        	Transaction_Bean prop = new Transaction_Bean();
    		//prop.setDBManager();
    		
        	RequestParameter req = new RequestParameter();
    		req.setUsername(username);
    		req.setMessage("System Message");
    		req.setPassword(password);
    		req.setRequest("transaction");
    		req.setProcess("add");
        	prop.setReqParam(req);
        	
        	Hashtable<Object, Object> old = new Hashtable<Object, Object>();
    		old.put("Transaction_type", get.getItem(transaction_type));
    		old.put("Buyer", buyer);
    		old.put("seller", seller);
    		old.put("delivery_date", delivery_date);
    		
    		//prop.setOldTransactionHash(old);
    		prop.setOldHashtable(old);
        	String itemlist = request.getParameter("itemlist");
        	
        	String [] ordered_items = itemlist.split(String.valueOf("\t"));
        	ResultSet rs = null;
        	
        	for(int i=0;i<ordered_items.length;i++) 
        	{
        		System.out.println(ordered_items[i]);
        		String [] temp = ordered_items[i].split(String.valueOf(":"));
        		
        		try
        		{
	        		String item = temp[0];
	        		String qty = temp[1];
		            
		            System.out.println(item);
		            System.out.println(qty);
		            
		            Hashtable<Object, Object> item_qty = new Hashtable<Object, Object>();
		            
		            item_qty.put("item",rs.getString("PV"));
			        item_qty.put("qty",qty);
			        System.out.println(item_qty.toString());
			        
			        // prop.setTransDetailHash(item_qty);
			        prop.setTransDetailsViewHashtable(item_qty);
        		}
        		catch(Exception e)
        		{
        			e.printStackTrace();
        		}
			}
        	
    		//prop.setReqParam(req);    		
    		//prop.insertTransaction();
        	prop.insert();
    		
    		String temprs = null;
    		int i = 0;
    		
    		// needs to change because it will not return correct transaction id.
    		
    		try
    		{
	    		rs = DBManager.getSelect("select id from max_id_table where table_name = 'transaction'");
	    		rs.next();
	    		temprs = rs.getString("id");
	    		i = Integer.parseInt(temprs)-1;
    		}
    		catch (Exception e) 
    		{
				e.printStackTrace();
			}
    		finally
    		{
    			rs = null;
    		}
    		
    		response.sendRedirect("ViewServlet?objectname="+i+"&param=id");
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally
		{
			get = null;
		}
	}   	  	    
}